import axios, { AxiosRequestConfig, Method } from 'axios';

const devUrl = 'http://localhost:8068';
const proUrl = 'http://124.70.63.154:8068';

export const BASE_URL: string = process.env.NODE_ENV === 'development' ? devUrl : proUrl;
export const requestMethod: { POST: Method; GET: Method } = {
  POST: 'POST',
  GET: 'GET',
};
const instance = axios.create({
  baseURL: BASE_URL,
});
instance.interceptors.request.use(
  (config: any) => {
    return config;
  },
  (error: any) => {
    return error;
  },
);
instance.interceptors.response.use(
  (res: { data: any }) => {
    return res.data;
  },
  (error: any) => {
    return Promise.reject(error);
  },
);

export function useRequest(config: AxiosRequestConfig | string): Promise<{ code: number; data: any; message: string }> {
  if (typeof config === 'object') {
    return instance.request(config);
  } else {
    return instance.get(config);
  }
}
